//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Platforms/Base/IApplication.h"

#if defined( PLATFORM_WINDOWS )


namespace Engine
{
namespace Platforms
{
	using namespace winapi;

#	include <stl_ext/GL/wglext.h>



	//
	// Windows OpenGL Library
	//

	struct WindowsLibOpenGL
	{
	// types
	private:
		typedef PROC (CALLBACK * wglGetProcAddressProc_t) (LPCSTR lpszProc);


	// variables
	private:
		Library						_lib;
		wglGetProcAddressProc_t		_getProc;


	// methods
	public:
		WindowsLibOpenGL () : _getProc(null)
		{}

		bool Load (uni_c_string name = uni_c_string())
		{
			if ( not name.Empty() )
			{
				if ( _lib.Load( name, true ) )
					return _OnInit();
			}

			if ( _lib.Load( GetDefaultName(), true ) )
				return _OnInit();

			return false;
		}

		void Unload ()
		{
			_lib.Unload();
		}

		void * GetProc (uni_c_string address) const
		{
			void * res = null;

			if ( _getProc != null and (res = _getProc( address.cstr() )) != null )
				return res;
			
			if ( (res = _lib.GetProc( address )) != null )
				return res;

			return null;
		}

		static uni_c_string  GetDefaultName ()
		{
#		ifdef USE_ANGLE_GLES
			return "libGLESv2.dll";
#		else
			return "opengl32.dll";
#		endif
		}


	private:
		
		bool _OnInit ()
		{
			_getProc = (wglGetProcAddressProc_t) _lib.GetProc( "wglGetProcAddress" );
			return true; //_getProc != null;
		}
		/*
		static bool _IsCoreFunc (uni_c_string name)
		{
			static const set< string >	func_set = ( set< string >()
					<< "glBindTexture" << "glBlendFunc" << "glClear" << "glClearColor" << "glClearDepth"
					<< "glClearStencil" << "glColorMask" << "glCullFace" << "glCopyTexImage1D"
					<< "glCopyTexImage2D" << "glCopyTexSubImage1D" << "glCopyTexSubImage2D"
					<< "glDeleteTextures" << "glDepthFunc" << "glDepthMask" << "glDepthRange"
					<< "glDisable" << "glDrawArrays" << "glDrawBuffer" << "glDrawElements"
					<< "glEnable" << "glFinish" << "glFlush" << "glFrontFace" << "glGenTextures"
					<< "glGetBooleanv" << "glGetDoublev" << "glGetFloatv" << "glGetIntegerv"
					<< "glGetError" << "glGetString" << "glGetTexImage" << "glGetTexLevelParameterfv"
					<< "glGetTexLevelParameteriv" << "glGetTexParameterfv" << "glGetTexParameteriv"
					<< "glHint" << "glIsEnabled" << "glIsTexture" << "glLineWidth" << "glLogicOp"
					<< "glPixelStoref" << "glPixelStorei" << "glPointSize" << "glPolygonMode"
					<< "glPolygonOffset" << "glReadBuffer" << "glReadPixels" << "glScissor"
					<< "glStencilFunc" << "glStencilMask" << "glStencilOp" << "glTexImage1D"
					<< "glTexImage2D" << "glTexSubImage1D" << "glTexSubImage2D" << "glTexParameterf"
					<< "glTexParameterfv" << "glTexParameteri" << "glTexParameteriv" << "glViewport" );

			const string *	res = null;

			return func_set.Find( name, res );
		}*/
	};


}	// Platforms


namespace Base
{
	typedef Platforms::WindowsLibOpenGL		LibOpenGL;

}	// Base
}	// Engine

#endif	// PLATFORM_WINDOWS